package com.example.prueba1pds;


import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import com.google.gson.JsonObject;




import objeto.Paciente;
import webservice.Constantes;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.backup.RestoreObserver;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UpdatePaciente extends Activity {
	
	// Constantes para la invocacion del web service
	private String URL = Constantes.HOST
			+ "/Prueba1PdsWS/services/PacienteORMWS";
	private String NAMESPACE = "http://ws";

	Paciente paciente;
	EditText nombreEditText;
	EditText rutEditText;
	EditText digitoVerificadorEditText;
	EditText direccionEditText;
	Button btnActualizar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_paciente);
        
        nombreEditText = (EditText) findViewById(R.id.update_paciente_nombre);
        rutEditText =  (EditText) findViewById(R.id.update_paciente_rut);
        digitoVerificadorEditText =  (EditText) findViewById(R.id.update_paciente_digitoverificador);
        direccionEditText =  (EditText) findViewById(R.id.update_paciente_direccion);
        
        
        paciente = (Paciente) getIntent().getExtras().getSerializable("paciente");
		Log.d("", paciente.toString());
        
        btnActualizar = (Button) findViewById(R.id.btnUpdate);

        btnActualizar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Log.d("onclick","1");
				JsonObject jsonObjectPaciente = new JsonObject();
				Log.d("onclick","2");
				
				jsonObjectPaciente.addProperty("id", paciente.getId());
				
				Log.d("onclick","3");
				jsonObjectPaciente.addProperty("nombre", nombreEditText.getText().toString());
				Log.d("onclick","3");
				jsonObjectPaciente.addProperty("rut", rutEditText.getText().toString());
				Log.d("onclick","4");
				jsonObjectPaciente.addProperty("digitoVerificador", digitoVerificadorEditText.getText().toString());
				jsonObjectPaciente.addProperty("direccion", direccionEditText.getText().toString());

				Log.d("JsonObject",jsonObjectPaciente.toString());
				new UpdatePacienteBackground().execute(jsonObjectPaciente.toString());
			}
		});
        
        		
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_update_paciente, menu);
        return true;
    }
    
    

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.borrar_paciente:
            	Log.d("paciente id; ",""+paciente.getId());
            	new RemovePacienteBackground().execute(paciente.getId());
            	return true;
            default:
                return true;
        }
    }
    
    
    
    
	class UpdatePacienteBackground extends AsyncTask<String, Void, String>{

		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(
					UpdatePaciente.this,
					"Actualizando Paciente", 
					"Espere mientras se actualizando el paciente...", 
					true);
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {

			String resultado = new String();
			String METHOD_NAME = "updatePaciente";
			String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			request.addProperty("jsonPaciente", params[0]);

			HttpTransportSE aht = new HttpTransportSE(URL);

			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
				resultado = resultString.toString();
				Log.d("info resultado",resultado);

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultado;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			//Valido que tenga la informacion necesaria
			if("1".equals(result)){
				Toast.makeText(UpdatePaciente.this, "Ingresada correctamente", Toast.LENGTH_LONG).show();
				finish();
			}else{
				Toast.makeText(UpdatePaciente.this, "Error al obtener la informacion necesaria", Toast.LENGTH_LONG).show();
			}
		}
	}

	class RemovePacienteBackground extends AsyncTask<Integer, Void, String>{

		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(
					UpdatePaciente.this,
					"Eliminacion Paciente", 
					"Espere mientras se elimina el Paciente...", 
					true);
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Integer... params) {

			String resultado = new String();
			String METHOD_NAME = "removePaciente";
			String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			request.addProperty("pacienteId", params[0]);

			HttpTransportSE aht = new HttpTransportSE(URL);

			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
				resultado = resultString.toString();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultado;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			//Valido que tenga la informacion necesaria
			if("1".equals(result)){
				Toast.makeText(UpdatePaciente.this, "Eliminada correctamente", Toast.LENGTH_LONG).show();
				finish();
			}else{
				Toast.makeText(UpdatePaciente.this, "Error al obtener la informacion necesaria", Toast.LENGTH_LONG).show();
			}
		}
	}


    
    
    
}
